package com.zhongshengnetwork.rightbe.wzt.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wang.avi.AVLoadingIndicatorView;
import com.zhongshengnetwork.rightbe.Constant.APIKey;
import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.GsonTools;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;
import com.zhongshengnetwork.rightbe.common.TopBarView;
import com.zhongshengnetwork.rightbe.common.activity.AppBaseActivity;
import com.zhongshengnetwork.rightbe.common.customview.ActionSheetDialog;
import com.zhongshengnetwork.rightbe.common.customview.CustomDialog;
import com.zhongshengnetwork.rightbe.common.utils.ToastUtil;
import com.zhongshengnetwork.rightbe.gsonmodel.ClassifyModel;
import com.zhongshengnetwork.rightbe.gsonmodel.CommonModel;
import com.zhongshengnetwork.rightbe.gsonmodel.WeiShuInfoDetailModel;
import com.zhongshengnetwork.rightbe.https.HttpCallBack;
import com.zhongshengnetwork.rightbe.https.HttpsUtils;
import com.zhongshengnetwork.rightbe.lang.activity.LangPublishActivity;
import com.zhongshengnetwork.rightbe.lang.activity.SelfImagePreviewActivity;
import com.zhongshengnetwork.rightbe.wzt.model.WZTUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.huantansheng.easyphotos.constant.Code.REQUEST_PERMISSION;

public class WZTListOfClassifyActivity extends AppBaseActivity implements TopBarView.onTitleBarClickListener{

    private static final int REQUEST_SELECT = 5002;
    private static final int CLASSIFY_PUBLISH = 5003;
    private TopBarView topbar;
    private String title;
    private int opType = 0; //操作类型：0- 微纸条列表；1-选择未归类微纸条
    private ClassifyModel classifyModel = null;
    private String dataContent = "";
    private final int linecount = 3;
    public String userid = null;
    private int pageindex = 0;
    private int imgW = 0;
    private static final int Records = 20;
    private boolean isGetting = false;
    private boolean isHasMore = false;
    private List<WeiShuInfoDetailModel> list;
    private ListView listview;
    private MyAdapter adapter;
    //加载动画
    private AVLoadingIndicatorView avi;
    private RefreshLayout mRefreshLayout;

    private ImageView no_data_img;
    private Button publish_button;
    private boolean isPublish = false;
    private boolean isHome = false;
    private int dataId = 0;

    private EditText search_edit;
    private Button search_button;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT && resultCode == RESULT_OK){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mRefreshLayout.autoRefresh();
                }
            });
        }else if (requestCode == CLASSIFY_PUBLISH && resultCode == RESULT_OK){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setResult(RESULT_OK);
                    finish();
                }
            });
        }
    }

    private void moreMenu(){
        final List<String> menu = new ArrayList<>();
        menu.add("添加微纸条");
        menu.add("编辑分类");
        if (classifyModel.getVisible() == true){
            menu.add("分享分类");
        }
        if (classifyModel.isNeedAuth() == true){
            menu.add("授权管理");
        }
        menu.add("删除分类");
        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(WZTListOfClassifyActivity.this)
                .builder()
                .setTitle("请选择")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (String s : menu){
            actionSheetDialog.addSheetItem(s, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which > 0){
                        which -= 1;
                        String title = menu.get(which);
                        if (title.equals("添加微纸条")){
                            Intent intent = new Intent(WZTListOfClassifyActivity.this, WZTListOfClassifyActivity.class);
                            intent.putExtra("opType", 1);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("model", classifyModel);//序列化
                            intent.putExtras(bundle);//发送数据
                            startActivityForResult(intent, REQUEST_SELECT);
                        }else if (title.equals("编辑分类")){
                            if (classifyModel != null){
//                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){
//                                    if (ContextCompat.checkSelfPermission(WZTListOfClassifyActivity.this, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED){
//                                        ActivityCompat.requestPermissions(WZTListOfClassifyActivity.this, new String[]{Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_IMAGES},
//                                                MainActivity.REQUEST_PERMISSION);
//                                        return;
//                                    }
//                                }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
//                                        && ContextCompat.checkSelfPermission(WZTListOfClassifyActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//                                    ActivityCompat.requestPermissions(WZTListOfClassifyActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
//                                            REQUEST_PERMISSION);
//                                    ToastUtil.show(WZTListOfClassifyActivity.this,"亲，请开启应用对SD卡的读写权限");
//                                    return;
//                                }
                                Intent intent = new Intent(WZTListOfClassifyActivity.this, NewClassifyActivity.class);
                                Bundle bundle=new Bundle();
                                bundle.putSerializable("model", classifyModel);//序列化
                                intent.putExtras(bundle);//发送数据
                                startActivityForResult(intent, CLASSIFY_PUBLISH);
                            }
                        }else if (title.equals("授权管理")){
                            Intent intent = new Intent(WZTListOfClassifyActivity.this, ClassifyAuthActivity.class);
                            intent.putExtra("dataType",1);
                            intent.putExtra("dataId", classifyModel.getClassifyId()+"");
                            startActivity(intent);
                        }else if(title.equals("分享分类")){
                            Intent intent = new Intent(WZTListOfClassifyActivity.this, ClassifyShareCodeActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("model", classifyModel);//序列化
                            intent.putExtras(bundle);//发送数据
                            startActivity(intent);
                        } else if (title.equals("删除分类")){
                            CustomDialog dialog = null;
                            final CustomDialog.Builder builder = new CustomDialog.Builder(WZTListOfClassifyActivity.this);
                            builder.setTitle("温馨提示");
                            builder.setMessage("是否要删除分类："+classifyModel.getClassifyName()+"？");
                            builder.setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                    deleteClassify();
                                }
                            });
                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                            dialog = builder.create();
                            dialog.show();
                        }
                    }
                }
            });
        }
        actionSheetDialog.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wztlist_of_classify);
        avi = (AVLoadingIndicatorView)findViewById(R.id.avi);
        opType = getIntent().getIntExtra("opType", 0);
        classifyModel =  (ClassifyModel) getIntent().getSerializableExtra("model");

        topbar = (TopBarView)findViewById(R.id.topbar);
        topbar.getRightView().setVisibility(View.GONE);
        topbar.setClickListener(this);
        topbar.getTitleView().setClickable(true);
        if (classifyModel != null){
            topbar.getTitleView().setText(classifyModel.getClassifyName());
        }else {
            topbar.getTitleView().setText("选择微纸条");
        }
        topbar.getTitleView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list != null && list.size() > 0){
                    //listview.smoothScrollToPosition(0);
                    listview.setSelection(0);
                }
            }
        });
        if (opType == 0 && CommonUtils.isLogin() == true && classifyModel != null){
            if (classifyModel.getUserid().equals(CustomApplication.loginModel.getUid()) ){
                //添加微纸条
                topbar.getRightView().setImageResource(R.drawable.more);
                topbar.getRightView().setVisibility(View.VISIBLE);
                topbar.getRightView().setClickable(true);
                topbar.getRightView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        moreMenu();
                    }
                });
            }
        }
        pageindex = 0;
        if (list == null){
            list = new ArrayList<WeiShuInfoDetailModel>();
            int width = ScreenUtils.getScreenWidth(WZTListOfClassifyActivity.this);
            float density = ScreenUtils.getScreenDensity(WZTListOfClassifyActivity.this);
            imgW = (int) ((width-40*density)/linecount);
        }
        listview=(ListView) findViewById(R.id.wzt_listview);
        adapter=new MyAdapter();
        listview.setAdapter(adapter);

        no_data_img = (ImageView)findViewById(R.id.no_data_img);
        publish_button = (Button) findViewById(R.id.publish_button);
        publish_button.setClickable(true);
        publish_button.setBackgroundResource(AppThemeUtils.getInstance().getButtonBg());
        publish_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                moreMenu();
            }
        });
        mRefreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        //设置 Header 为 贝塞尔雷达 样式
        mRefreshLayout.setRefreshHeader(new BezierRadarHeader(WZTListOfClassifyActivity.this).setEnableHorizontalDrag(true));//BezierRadarHeader
        //设置 Footer 为 球脉冲 样式
        mRefreshLayout.setRefreshFooter(new ClassicsFooter(WZTListOfClassifyActivity.this).setSpinnerStyle(SpinnerStyle.FixedBehind));//BallPulseFooter ClassicsFooter
        mRefreshLayout.setPrimaryColors(AppThemeUtils.getInstance().getRefreshBackgroundColor(),AppThemeUtils.getInstance().getRefreshAccessColor());
        mRefreshLayout.setEnableRefresh(true);//启用刷新
        mRefreshLayout.setEnableLoadMore(true);//启用加载
        //刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //refreshlayout.finishRefresh();
                pageindex = 0;
                getData();
            }
        });
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //refreshLayout.finishLoadMore();
                getData();
            }
        });

        search_edit = (EditText) findViewById(R.id.search_edit);
        search_button = (Button) findViewById(R.id.search_button);
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickSearch(v);
            }
        });

        RelativeLayout search_top = (RelativeLayout) findViewById(R.id.search_top);
        search_top.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        RelativeLayout search_shape_layout = (RelativeLayout) findViewById(R.id.search_shape_layout);
        search_shape_layout.setBackgroundResource(AppThemeUtils.getInstance().getSearchBackground());
        EditText search_edit = (EditText) findViewById(R.id.search_edit);
        search_edit.setHintTextColor(AppThemeUtils.getInstance().getLightTextColor());
        search_edit.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        RelativeLayout wztlist_of_classify = (RelativeLayout) findViewById(R.id.wztlist_of_classify);
        wztlist_of_classify.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        listview.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
        listview.setDivider(new ColorDrawable(AppThemeUtils.getInstance().getMainColor()));
        listview.setDividerHeight(AppThemeUtils.DividerHeight);

        mRefreshLayout.autoRefresh();

    }

    private void deleteClassify(){
        if (classifyModel != null){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final HashMap<String, String> map = new HashMap<String, String>();
                    String url = "weishu/classify/delete.do";
                    map.put(APIKey.tokenKey, CommonUtils.formatString(CustomApplication.loginModel.getToken()));
                    map.put("classifyId", "" + classifyModel.getClassifyId());
                    HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                        @Override
                        public void onSuccess(String data) {
                            CommonModel commonModel = GsonTools.getCommonModel(data);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (commonModel.getFlag().equals("1")){
                                        ToastUtil.show(WZTListOfClassifyActivity.this, "删除成功");
                                        setResult(RESULT_OK);
                                        finish();
                                    }else {
                                        CustomApplication.showTip(commonModel, WZTListOfClassifyActivity.this);
                                    }
                                }
                            });
                        }

                        @Override
                        public void onError(String msg) {

                        }
                    });
                }
            }){

            }.start();
        }
    }

    public void hintKeyBoard() {
        //拿到InputMethodManager
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        //如果window上view获取焦点 && view不为空
        if (imm.isActive() && getCurrentFocus() != null) {
            //拿到view的token 不为空
            if (getCurrentFocus().getWindowToken() != null) {
                //表示软键盘窗口总是隐藏，除非开始时以SHOW_FORCED显示。
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    private void onClickSearch(View view){
        hintKeyBoard();
        String text = search_edit.getText().toString();
        dataContent = text;
        if (mRefreshLayout != null){
            Log.e("TAG","进来获取数据了刷新界面");
                /*
                @param delayed 开始延时
                @param duration 拖拽动画持续时间
                @param dragRate 拉拽的高度比率（要求 ≥ 1 ）
                */
            mRefreshLayout.autoRefresh(100,300,1f,false);
        }
    }

    private void getData() {
        if (listview == null) {
            return;
        }
        if (isGetting == true) {
            return;
        }
        isGetting = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                final HashMap<String, String> map = new HashMap<String, String>();
                String url = "weishu/classify/wzt/list.do";
                if (opType == 1){
                    url = "weishu/classify/wzt/original/list.do";
                }
                if (classifyModel != null){
                    map.put("dataId",classifyModel.getClassifyId()+"");
                }
                map.put("dataContent", CommonUtils.formatString(dataContent));
                map.put(APIKey.tokenKey, CommonUtils.formatString(CustomApplication.loginModel.getToken()));
                map.put("pageIndex",pageindex+"");
                map.put("pageRecord",Records+"");
                HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
                    @Override
                    public void onSuccess(String data) {
                        isGetting = false;
                        CommonModel commonModel = GsonTools.getCommonModel(data);
                        if (commonModel.getFlag().equals("1")){
                            final List<WeiShuInfoDetailModel> l = GsonTools.getWeiShuInfoDetailModel(data);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Loading){
                                        //listview.stopLoadMore();
                                        mRefreshLayout.finishLoadMore();
                                    }else {
                                        list.clear();
                                        //listview.stopRefresh(true);
                                        mRefreshLayout.finishRefresh();
                                    }
                                    if (l != null && l.size() > 0){
                                        no_data_img.setVisibility(View.INVISIBLE);
                                        publish_button.setVisibility(View.INVISIBLE);
                                        if (l.size() >= Records){
                                            //listview.setPullLoadEnable(true);
                                            mRefreshLayout.setEnableLoadMore(true);
                                        }else {
                                            //listview.setPullLoadEnable(false);
                                            mRefreshLayout.setEnableLoadMore(false);
                                        }
                                        list.addAll(l);
                                    }else {
                                        isHasMore = false;
                                        //listview.setPullLoadEnable(false);
                                        mRefreshLayout.setEnableLoadMore(false);
                                    }
                                    adapter.notifyDataSetChanged();
                                    if (pageindex == 0){
                                        //listview.smoothScrollToPosition(0);
                                        listview.setSelection(0);
                                        if (list.size() == 0){
                                            if (opType == 0 && CommonUtils.isLogin() == true && classifyModel != null && classifyModel.getUserid().equals(CustomApplication.loginModel.getUid())){
                                                no_data_img.setVisibility(View.INVISIBLE);
                                                publish_button.setVisibility(View.VISIBLE);
                                            }else {
                                                no_data_img.setVisibility(View.VISIBLE);
                                                publish_button.setVisibility(View.INVISIBLE);
                                            }
                                        }
                                    }
                                    if (l != null && l.size() > 0){
                                        pageindex += 1;
                                    }
                                }
                            });
                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (mRefreshLayout.getState() == RefreshState.Loading){
                                        //listview.stopLoadMore();
                                        mRefreshLayout.finishLoadMore();
                                    }else {
                                        list.clear();
                                        //listview.stopRefresh(true);
                                        mRefreshLayout.finishRefresh();
                                    }
                                    CustomApplication.showTip(commonModel, WZTListOfClassifyActivity.this);
                                }
                            });
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        isGetting = false;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mRefreshLayout.getState() == RefreshState.Loading){
                                    //listview.stopLoadMore();
                                    mRefreshLayout.finishLoadMore();
                                }else {
                                    mRefreshLayout.finishRefresh();
                                }
                            }
                        });
                    }
                });
            }
        }){

        }.start();
    }

    /**
     * 点击了返回导航按钮
     */
    @Override
    public void onBackClick() {
        //Toast.makeText(LangDetailActivity.this, "你点击了左侧按钮", Toast.LENGTH_LONG).show();
        finish();
    }

    /**
     * 点击了右边导航按钮
     */
    @Override
    public void onRightClick() {

    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            int count = list.size()/linecount;
            if (list.size()%linecount > 0){
                count += 1;
            }
            return count;
        }


        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return super.isEnabled(position);
        }

        /**
         * @param position
         * @param convertView
         * @param parent
         * @return
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //convertView的复用
            if (convertView == null) {
                convertView = View.inflate(WZTListOfClassifyActivity.this, R.layout.wzt_listview_item,null);
            }
            View view=convertView;

            LinearLayout wzt_layout_1 = (LinearLayout) view.findViewById(R.id.wzt_layout_1);
            LinearLayout wzt_layout_2 = (LinearLayout) view.findViewById(R.id.wzt_layout_2);
            LinearLayout wzt_layout_3 = (LinearLayout) view.findViewById(R.id.wzt_layout_3);

            ImageView wzt_img1 = (ImageView) view.findViewById(R.id.wzt_img1);
            TextView wzt_readcount_1 = (TextView) view.findViewById(R.id.wzt_readcount_1);
            TextView private_text_1 = (TextView) view.findViewById(R.id.private_text_1);
            TextView wzt_text1 = (TextView) view.findViewById(R.id.wzt_text1);


            ImageView wzt_img2 = (ImageView) view.findViewById(R.id.wzt_img2);
            TextView wzt_readcount_2 = (TextView) view.findViewById(R.id.wzt_readcount_2);
            TextView private_text_2 = (TextView) view.findViewById(R.id.private_text_2);
            TextView wzt_text2 = (TextView) view.findViewById(R.id.wzt_text2);

            ImageView wzt_img3 = (ImageView) view.findViewById(R.id.wzt_img3);
            TextView wzt_readcount_3 = (TextView) view.findViewById(R.id.wzt_readcount_3);
            TextView private_text_3 = (TextView) view.findViewById(R.id.private_text_3);
            TextView wzt_text3 = (TextView) view.findViewById(R.id.wzt_text3);

            LinearLayout wzt_listview_item_layout = (LinearLayout) view.findViewById(R.id.wzt_listview_item_layout);
            wzt_listview_item_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_text1.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_text2.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_text3.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
            wzt_img1.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_img2.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());
            wzt_img3.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

            wzt_img1.setVisibility(View.GONE);
wzt_layout_1.setVisibility(View.GONE);
            wzt_readcount_1.setVisibility(View.GONE);
            private_text_1.setVisibility(View.GONE);
            wzt_text1.setVisibility(View.GONE);


wzt_img2.setVisibility(View.GONE);
wzt_layout_2.setVisibility(View.GONE);
            wzt_readcount_2.setVisibility(View.GONE);
            private_text_2.setVisibility(View.GONE);
            wzt_text2.setVisibility(View.GONE);

            wzt_img3.setVisibility(View.GONE);
wzt_layout_3.setVisibility(View.GONE);
            wzt_readcount_3.setVisibility(View.GONE);
            private_text_3.setVisibility(View.GONE);
            wzt_text3.setVisibility(View.GONE);

            int num = 0;
            int count = linecount*position;
            if (list.size()-count >= linecount){
                num = linecount;
            }else {
                while (list.size()-count > 0){
                    num+=1;
                    count+=1;
                }
            }
            int index = linecount*position;
            RequestOptions options = new RequestOptions().error(AppThemeUtils.getInstance().getDefaultImageBg()).bitmapTransform(new RoundedCorners(Constant.CornerRadius));//图片圆角
            for (int i = 0; i < num; i++){
                WeiShuInfoDetailModel model = list.get(index+i);
                if (i == 0){
                    wzt_img1.setVisibility(View.VISIBLE);
wzt_layout_1.setVisibility(View.VISIBLE);
                    wzt_readcount_1.setVisibility(View.VISIBLE);
                    wzt_text1.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams params = wzt_img1.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img1.setLayoutParams(params);
                    params = wzt_text1.getLayoutParams();
                    params.width = imgW;
                    wzt_text1.setLayoutParams(params);
                    if (WZTListOfClassifyActivity.this != null){
                        try{
                            Glide.with(WZTListOfClassifyActivity.this).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img1);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("分类下微纸条列表加载图片报错："+e.getLocalizedMessage());
                        }
                    }
                    wzt_text1.setText(model.getTitle());
                    wzt_readcount_1.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_1.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_1.setTag(index+i);
                    wzt_layout_1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }else if (i == 1){
                    wzt_img2.setVisibility(View.VISIBLE);
wzt_layout_2.setVisibility(View.VISIBLE);
                    wzt_readcount_2.setVisibility(View.VISIBLE);
                    wzt_text2.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams params = wzt_img2.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img2.setLayoutParams(params);
                    params = wzt_text2.getLayoutParams();
                    params.width = imgW;
                    wzt_text2.setLayoutParams(params);
                    if (WZTListOfClassifyActivity.this != null){
                        try{
                            Glide.with(WZTListOfClassifyActivity.this).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img2);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("分类下微纸条列表加载图片报错："+e.getLocalizedMessage());
                        }
                    }
                    wzt_text2.setText(model.getTitle());
                    wzt_readcount_2.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_2.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_2.setTag(index+i);
                    wzt_layout_2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }else if (i == 2){
                    wzt_img3.setVisibility(View.VISIBLE);
wzt_layout_3.setVisibility(View.VISIBLE);
                    wzt_readcount_3.setVisibility(View.VISIBLE);
                    wzt_text3.setVisibility(View.VISIBLE);
                    ViewGroup.LayoutParams params = wzt_img3.getLayoutParams();
                    params.width =imgW;
                    params.height = imgW;
                    wzt_img3.setLayoutParams(params);
                    params = wzt_text3.getLayoutParams();
                    params.width = imgW;
                    wzt_text3.setLayoutParams(params);
                    if (WZTListOfClassifyActivity.this != null){
                        try{
                            Glide.with(WZTListOfClassifyActivity.this).load(model.getCover()).apply(options).placeholder(AppThemeUtils.getInstance().getDefaultImageBg()).dontAnimate().into(wzt_img3);
                        }catch (Exception e){
                            LangPublishActivity.uploadError("分类下微纸条列表加载图片报错："+e.getLocalizedMessage());
                        }
                    }
                    wzt_text3.setText(model.getTitle());
                    wzt_readcount_3.setText(CommonUtils.getDisplayCount(model.getRealcount()));
                    if (model.getVisible() == false){
                        private_text_3.setVisibility(View.VISIBLE);
                    }
                    wzt_layout_3.setTag(index+i);
                    wzt_layout_3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            onClickItem(v);
                        }
                    });
                }
            }
            return view;
        }
    }

    private void onClickItem(View v){
        if (isPublish == true){
            return;
        }
        int index = (int)v.getTag();
        if (list == null || index >= list.size()){
            return;
        }
        WeiShuInfoDetailModel model = list.get(index);
      if (opType == 1){
            //选择微纸条
            addWZT(model);
        }else {
            if (CommonUtils.isLogin() == true && CustomApplication.loginModel.getUid().equals(model.getUserid())){
                Intent intent = new Intent(WZTListOfClassifyActivity.this, WZTActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("model", model);//序列化
                intent.putExtras(bundle);//发送数据
                intent.putExtra("isDeleteClassify", true);
                startActivityForResult(intent, REQUEST_SELECT);
            }else {
                WZTUtils.goToWZT(WZTListOfClassifyActivity.this, model);
            }
        }
    }

    private void addWZT(WeiShuInfoDetailModel model){
        //avi.show();
        final HashMap<String, String> map = new HashMap<String, String>();
        String url = "weishu/classify/wzt/add.do";
        map.put(APIKey.tokenKey, CommonUtils.formatString(CustomApplication.loginModel.getToken()));
        map.put("classifyId", ""+classifyModel.getClassifyId());
        map.put("weishuId", ""+model.getId());
        HttpsUtils.miniAppDo(map, url, new HttpCallBack() {
            @Override
            public void onSuccess(String data) {
                CommonModel commonModel = GsonTools.getCommonModel(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //avi.hide();
                        if (commonModel.getFlag().equals("1")){
                            ToastUtil.show(WZTListOfClassifyActivity.this, "添加成功");
                            setResult(RESULT_OK);
                            finish();
                        }else {
                            CustomApplication.showTip(commonModel, WZTListOfClassifyActivity.this);
                        }
                    }
                });
            }

            @Override
            public void onError(String msg) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //avi.hide();
                        ToastUtil.show(WZTListOfClassifyActivity.this, "添加失败，请稍后尝试~");
                    }
                });
            }
        });
    }

}

